class HttpManager {
	private static m_unTouchLayer:eui.Panel = null;

	public static postHttp(_param?:string, _onComplete?:Function, _onIOError?:Function, _onProgress?:Function):egret.HttpRequest
	{
		if(HttpManager.m_unTouchLayer == null)
		{
			HttpManager.m_unTouchLayer = new eui.Panel();
			HttpManager.m_unTouchLayer.x = 0.0;
			HttpManager.m_unTouchLayer.y = 0.0;
			HttpManager.m_unTouchLayer.width = S_WIDTH;
			HttpManager.m_unTouchLayer.height = S_HEIGHT;
		}
		if(HttpManager.m_unTouchLayer)
		{
			if(HttpManager.m_unTouchLayer.parent)
			{
				HttpManager.m_unTouchLayer.parent.removeChild(HttpManager.m_unTouchLayer);
			}
			SceneManager.getInstance().getRunningScene().addChild(HttpManager.m_unTouchLayer);
		}

		let request = new egret.HttpRequest();
		request.responseType = egret.HttpResponseType.TEXT;
		//设置为 POST 请求
		request.open("http://localhost:8586",egret.HttpMethod.POST);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.setRequestHeader
		request.send(_param);

		request.addEventListener(egret.Event.COMPLETE, function(_event:egret.Event)
		{
			if(HttpManager.m_unTouchLayer.parent)
			{
				HttpManager.m_unTouchLayer.parent.removeChild(HttpManager.m_unTouchLayer);
			}

			let request = <egret.HttpRequest>_event.currentTarget;
			if(HttpManager.getRespType(request.response) == REQ_TYPE_TOAST)
			{
				//toast
				let data = HttpManager.getRespData(request.response);
				let dataArray = HttpManager.getDataArray(data);

				const pbToast = ProtoManager.protoBufRoot.lookupType("GameModule.PBToast");
				let decodedData = pbToast.decode(dataArray).toJSON();
				new Toast(decodedData["msg"]);
			}

			if(_onComplete)
			{
				_onComplete(_event);
			}
		}, this);
		if(_onIOError)
		{
			request.addEventListener(egret.IOErrorEvent.IO_ERROR, _onIOError, this);
		}
		if(_onProgress)
		{
			request.addEventListener(egret.ProgressEvent.PROGRESS, _onProgress, this);
		}

		return request;
	}

	public static getParams(_type:number, _data?:string):string
	{
		let params = REQ_TYPE + "=" + _type;
		if(_data)
		{
			params += "&" + REQ_DATA + "=" + _data;
		}
		return params;
	}

	public static getRespType(_response:string):number
	{
		let respType = _response.split("&")[0];
		return +respType.split("=")[1];
	}

	public static getRespData(_response:string):string
	{
		let respData = _response.split("&")[1];
		return respData.split("=")[1];
	}

	public static getDataArray(_data:string):Uint8Array
	{
		let datas = _data.split(",");
		let dataArray = new Uint8Array(datas.length);
		for (var i = 0; i < datas.length; i ++) 
		{
			dataArray[i] = +datas[i];
		}
		return dataArray;
	}
}